package com.tg.controller;

import com.tg.pojo.Message;
import com.tg.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chats")
public class ChatController {
    @Autowired
    private ChatService chatService;

    @GetMapping("/{fromUsername}/{toUsername}")
    public Result getMessageHistoryList(@PathVariable("fromUsername") String fromUsername,@PathVariable("toUsername") String toUsername){
        List<Message> messageHistoryList = chatService.getMessageHistoryList(fromUsername, toUsername);
        return new Result(Code.GET_OK,messageHistoryList);
    }

    @GetMapping("/news/{username}")
    public Result getMessageList(@PathVariable String username) {
        List<Message> messageList = chatService.getMessageList(username);
        return new Result(Code.GET_OK,messageList);
    }

    @PostMapping
    public Result saveMessage(@RequestBody Message message){
        boolean flag = chatService.saveMessage(message);
        return new Result(flag ? Code.SAVA_OK : Code.SAVA_ERR);
    }
}
